package com.web.admin.web.base.user;

import com.blue.core.dict.HttpMethod;
import com.blue.core.dict.annotation.DictParser;
import com.blue.http.annotation.Http;
import com.blue.http.message.HttpRequest;
import com.blue.http.message.View;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.core.facade.BankFacade;
import com.web.base.join.BankJoinHandler;
import com.web.base.user.facade.BankCardFacade;
import com.web.base.user.facade.UserFacade;
import com.web.base.user.message.BankCardRequest;
import com.web.base.user.message.BankCardResponse;
import com.web.base.user.message.UserResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.validation.ValidationException;
import java.util.List;

/**
 * 银行卡控制器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年06月26日
 */
@Controller("userBankCardController")
@Http(url = "/base/user")
public class BankCardController
{
	@Autowired
	private BankCardFacade bankCardFacade;
	@Autowired
	private UserFacade userFacade;
	@Autowired
	private BankFacade bankFacade;
	
	private static DictParser parser = DictParser.getInstance();
	
	public BankCardController()
	{
	}

	@Http(url = "/bank_card_list", method = HttpMethod.GET)
	public View listGet(HttpRequest request)
	{
		int userId = request.getQueryInt("userId", 0);
		if (userId <= 0)
			throw new ValidationException("用户ID不能为空");

		UserResponse userResponse = userFacade.get(userId);
		List<BankCardResponse> cardList = bankCardFacade.listByUser(userId, null);
		BankJoinHandler.bankJoin(cardList, bankFacade);
		return View.createBuilder().setView("/base/user/bank_card_list")
						.put("model", userResponse)
						.put("map", parser.getStringMap())
						.put("cardList", cardList)
						.build();
	}

    @Http(url = "/bank_card_get", method = HttpMethod.GET)
    public View get(HttpRequest request)
    {
        int id = request.getQueryInt("id", 0);
        BankCardResponse response = null;
        if (id > 0)
        {
            response = bankCardFacade.get(id);
	        BankJoinHandler.bankJoin(response, bankFacade);
        }
        return View.createBuilder().setView("/base/user/bank_card_get")
                        .put("model", response == null ? new BankCardResponse() : response)
                        .put("map", parser.getStringMap())
                        .build();
    }

    @Http(url = "/bank_card_save", method = HttpMethod.POST)
    public void save(HttpRequest request)
    {
        BankCardRequest bankCardRequest = request.getPostObject(BankCardRequest.class);
        BankCardResponse response = null;
        if (bankCardRequest.getId() == null)
        {
            ValidationUtil.valid(bankCardRequest, SaveModel.class);
            response = bankCardFacade.add(bankCardRequest);
        }
        else
        {
            ValidationUtil.valid(bankCardRequest, UpdateModel.class);
            response = bankCardFacade.update(bankCardRequest);
        }
    }

}
